﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;

namespace Auction_Boxing_2
{
    /* An input handler is generated for each player.
     * The input handler:
     *      - checks for button releases
     *      - fires an event corresponding to the button release
     *      
     * Each player, if using a keyboard, has their keyboard keys stored here.
     */

    public enum KeyPressed
    {
        Up,
        Down,
        Left,
        Right,
        Attack0,
        Attack1,
        Attack2,
        Attack3
    }

    public class Input_Handler
    {
        public bool isActive;

        //-----Keyboard-----//

        KeyboardState kbPrevious;
        KeyboardState kbCurrent;

        public Keys kbUp;
        public Keys kbDown;
        public Keys kbLeft;
        public Keys kbRight;

        public Keys kbAttack_0;
        public Keys kbAttack_1;
        public Keys kbAttack_2;
        public Keys kbAttack_3;

        //-----Gamepad-----//

        GamePadState gpPrevious;
        GamePadState gpCurrent;

        int player_index;

        bool isGamePad;


        //-----Events & Delegates-----//

        public delegate void InputPressHandler(int player_index, KeyPressed key);
        public delegate void KeyRealease(KeyPressed key);
        public delegate void KeyPress(KeyPressed key);

        public event InputPressHandler OnKeyRelease;
        public event InputPressHandler OnKeyDown;

        // Constructer
        public Input_Handler()
        {
            this.isActive = false;
        }

        public void Initialize(int playerNumber, bool isGamePad)
        {
            this.isGamePad = isGamePad;
            this.player_index = playerNumber;
            this.isActive = true;

            if (isGamePad)
            {

            }
            else // your a keyboard
            {
                switch (playerNumber)
                {
                    case 0:
                        kbUp = Keys.W;
                        kbDown = Keys.S;
                        kbLeft = Keys.A;
                        kbRight = Keys.D;

                        kbAttack_0 = Keys.Z;
                        kbAttack_1 = Keys.X;
                        kbAttack_2 = Keys.C;
                        kbAttack_3 = Keys.V;
                        break;

                    case 1:
                        kbUp = Keys.I;
                        kbDown = Keys.K;
                        kbLeft = Keys.J;
                        kbRight = Keys.L;

                        kbAttack_0 = Keys.N;
                        kbAttack_1 = Keys.M;
                        kbAttack_2 = Keys.OemComma;
                        kbAttack_3 = Keys.OemPeriod;
                        break;

                    case 2:
                        kbUp = Keys.Up;
                        kbDown = Keys.Down;
                        kbLeft = Keys.Left;
                        kbRight = Keys.Right;

                        kbAttack_0 = Keys.OemBackslash;
                        kbAttack_1 = Keys.Delete;
                        kbAttack_2 = Keys.End;
                        kbAttack_3 = Keys.PageDown;
                        break;

                    case 3:
                        kbUp = Keys.NumPad8;
                        kbDown = Keys.NumPad5;
                        kbLeft = Keys.NumPad4;
                        kbRight = Keys.NumPad6;

                        kbAttack_0 = Keys.NumPad1;
                        kbAttack_1 = Keys.NumPad2;
                        kbAttack_2 = Keys.NumPad3;
                        kbAttack_3 = Keys.Enter;
                        break;
                }
            }
        }

        public void Update()
        {
            if (isGamePad)
            {

            }
            else
            {
                //Update States
                kbPrevious = kbCurrent;
                kbCurrent = Keyboard.GetState();

                //Up Key held down?
                if (kbCurrent.IsKeyDown(kbUp))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Up);

                }
                //Down Key held down?
                if (kbCurrent.IsKeyDown(kbDown))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Down);

                }
                //Left Key held down?
                if (kbCurrent.IsKeyDown(kbLeft))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Left);
                }
                //Right Key held down?
                if (kbCurrent.IsKeyDown(kbRight))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Right);
                }

                if (kbCurrent.IsKeyDown(kbAttack_0))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Attack0);
                }
                if (kbCurrent.IsKeyDown(kbAttack_1))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Attack1);
                }
                if (kbCurrent.IsKeyDown(kbAttack_2))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Attack2);
                }
                if (kbCurrent.IsKeyDown(kbAttack_3))
                {
                    if (OnKeyDown != null)
                        OnKeyDown(player_index, KeyPressed.Attack3);
                }

                //Up Key Released?
                if (kbPrevious.IsKeyDown(kbUp) && kbCurrent.IsKeyUp(kbUp))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Up);

                }



                //------RELEASE-------//

                //Down Key Released?
                if (kbPrevious.IsKeyDown(kbDown) && kbCurrent.IsKeyUp(kbDown))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Down);

                }
                //Left Key Released?
                if (kbPrevious.IsKeyDown(kbLeft) && kbCurrent.IsKeyUp(kbLeft))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Left);
                }
                //Right Key Released?
                if (kbPrevious.IsKeyDown(kbRight) && kbCurrent.IsKeyUp(kbRight))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Right);
                }
                //Attack0 Key Released?
                if (kbPrevious.IsKeyDown(kbAttack_0) && kbCurrent.IsKeyUp(kbAttack_0))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Attack0);
                }

                if (kbPrevious.IsKeyDown(kbAttack_1) && kbCurrent.IsKeyUp(kbAttack_1))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Attack1);
                }
                if (kbPrevious.IsKeyDown(kbAttack_2) && kbCurrent.IsKeyUp(kbAttack_2))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Attack2);
                }
                if (kbPrevious.IsKeyDown(kbAttack_3) && kbCurrent.IsKeyUp(kbAttack_3))
                {
                    if (OnKeyRelease != null)
                        OnKeyRelease(player_index, KeyPressed.Attack3);
                }
            }
        }
    }
}
